package config

import (
	"fmt"
	"github.com/spf13/viper"
	"os"
	"path/filepath"
	"smmf/log"
	"strings"
)

var (
	//Host      string
	//AppName   string
	Address       string
	MysqlDsn      string
	RedisAddress  string
	RedisPassword string
)

//func init() {
//	Host = "qqq"
//	AppName = "smmf"
//}

func GetRootPath() string {
	return getCurrentDirectory()
}

func getCurrentDirectory() string {
	dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
	if err != nil {
		fmt.Println(err)
	}
	return strings.Replace(dir, "\\", "/", 0)
}

func InitConfig() {
	//viper.AddConfigPath(getCurrentDirectory() + "/conf")
	viper.AddConfigPath("/Users/zaige/Work/server/src/smmf/conf")
	viper.SetConfigName("app")
	viper.SetConfigType("yaml")
	viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
	viper.AutomaticEnv()
	if err := viper.ReadInConfig(); err != nil {
		if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
			panic(fmt.Errorf("Fatal error config file: %s", err))
		}
	}
	MysqlDsn = viper.GetString("mysql.dsn")
	Address = viper.GetString("server.address")
	RedisAddress = viper.GetString("redis.address")
	RedisPassword = viper.GetString("redis.password")
	//cfg.InitLogger(GetRootPath()+viper.GetString("log.filePath"))
	log.InitLogger("/Users/zaige/Work/server/src/smmf/" + viper.GetString("log.filePath"))

}
